<?php
/**
 * 管理员登录页面
 * 相对路径: adm/login.php
 * 作者: 15058593138@qq.com
 */

// 处理Ajax登录请求
if (isset($_GET['act']) && $_GET['act'] === 'login') {
    $phone = isset($_POST['phone']) ? safeStr($_POST['phone']) : '';
    $pwd = isset($_POST['pwd']) ? $_POST['pwd'] : '';
    
    if (empty($phone) || empty($pwd)) {
        jsonMsg(0, '请输入账号和密码');
    }
    
    $db = new DB();
    $userTable = TB_PREFIX . 'user';
    $pwdHash = pwdHash($pwd);
    
    $sql = "SELECT * FROM `{$userTable}` WHERE `手机号` = '{$phone}' AND `密码` = '{$pwdHash}' AND `VXID` = 'admin'";
    $user = $db->getRow($sql);
    
    if ($user) {
        // 更新最后登录时间
        $db->update($userTable, ['最后登录' => date('Y-m-d H:i:s')], "`id` = {$user['id']}");
        
        // 设置Session
        $_SESSION['user_id'] = $user['id'];
        $_SESSION['user_phone'] = $user['手机号'];
        $_SESSION['is_admin'] = 1;
        
        $db->close();
        jsonMsg(1, '登录成功', ['redirect' => 'admin.php?do=show']);
    } else {
        $db->close();
        jsonMsg(0, '账号或密码错误，或无管理员权限');
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理员登录 - 教室座位预定系统</title>
    <link rel="stylesheet" href="./inc/pubs.css?v=<?php echo VERSION; ?>">
    <style>
        body {
            display: flex;
            align-items: center;
            justify-content: center;
            min-height: 100vh;
            background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
        }
        .login-box {
            background: #fff;
            padding: 40px;
            border-radius: 10px;
            box-shadow: 0 10px 40px rgba(0,0,0,0.2);
            width: 90%;
            max-width: 400px;
        }
        .login-box h2 {
            text-align: center;
            color: #333;
            margin-bottom: 30px;
        }
        .login-box .logo {
            text-align: center;
            font-size: 48px;
            color: #f5576c;
            margin-bottom: 20px;
        }
    </style>
</head>
<body>
    <div class="login-box">
        <div class="logo"></div>
        <h2>管理员登录</h2>
        
        <form id="loginForm">
            <div class="form-group">
                <label>管理员账号</label>
                <input type="text" name="phone" class="form-control" placeholder="请输入管理员账号" required>
            </div>
            
            <div class="form-group">
                <label>密码</label>
                <input type="password" name="pwd" class="form-control" placeholder="请输入密码" required>
            </div>
            
            <div class="form-group">
                <button type="submit" class="btn btn-primary" style="width:100%">登 录</button>
            </div>
            
            <div style="text-align:center;">
                <a href="index.php" style="color:#666; font-size:12px; text-decoration:none;">返回用户端</a>
            </div>
        </form>
    </div>
    
    <script src="./inc/pubs.js?v=<?php echo VERSION; ?>"></script>
    <script>
        document.getElementById('loginForm').onsubmit = function(e) {
            e.preventDefault();
            
            const formData = getFormData('loginForm');
            
            ajax('admin.php?do=login&act=login', formData, function(res) {
                if (res.code === 1) {
                    toast(res.msg, 'success');
                    setTimeout(() => {
                        window.location.href = res.data.redirect;
                    }, 1000);
                } else {
                    toast(res.msg, 'error');
                }
            });
        };
    </script>
</body>
</html>